<?php

namespace App\Http\Admin\Services\Media\Wtt;

use App\Exceptions\InvalidRequestException;
use App\Http\Admin\Services\BaseService;
use App\Http\Models\MediaWtt;
use Illuminate\Support\Facades\DB;

class MediaService extends BaseService
{
    public function wttData($conditions = [])
    {
        //获取列表
        $query = MediaWtt::query();
        $query = $this->withConditions($query,$conditions);
        if(!empty($conditions['key'])){
            $query->orderBy($conditions['key'],$conditions['order']);
        }
        $list = $query->paginate(50);
        //统计数据
        $raw = DB::raw('account_name,
            sum(read_count) as total_read_count,
            sum(digg_count) as total_digg_count,
            sum(comment_count) as total_comment_count
        ');
        $query_sum = MediaWtt::select($raw);
        $query_sum = $this->withConditions($query_sum,$conditions);
        $data = $query_sum->groupBy('account_name')->get();
        return [
           'list' => $list,
           'data' => $data
        ];


    }

    public function withConditions($query,$conditions = [])
    {

        if(!empty($conditions['account_name'])){
            $query->where('account_name',$conditions['account_name']);
        }
        if(!empty($conditions['start_time'])){
            $query->where('create_time','>=',$conditions['start_time']);
        }
        if(!empty($conditions['end_time'])){
            $query->where('create_time','<=',$conditions['end_time']);
        }


        return $query;

    }

}

